#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int N;
	cin >> N;
	string a = "";
	for (int i = 0; i < N; i++)
	{
		cin >> a;
		int k = 0, m = 0, n = 0;
		while (a[k] != 0)
		{
			m = a[k] - '0';
			if (m > 8) m -= 2;
			else if (3 < m && m < 8) m -= 1;
			n = n * 8 + m;
			k++;
		}
		cout << n << endl;
	}
	return 0;
}